package com.bytech.admin.biz.controller.blog;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.Resource;

import com.bytech.common.biz.controller.base.BasicController;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bytech.common.biz.entity.base.BaseResult;
import com.bytech.common.biz.entity.blog.BlogInfoVo;
import com.bytech.common.biz.enums.ServiceTypeEnum;
import com.bytech.common.biz.enums.comment.CommentTypeEnum;
import com.bytech.common.biz.service.remote.article.RemoteArticleService;
import com.bytech.common.biz.service.remote.comment.RemoteCommentService;
import com.bytech.common.config.AppGlobalConstants;
import com.bytech.common.utils.DateUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
 * 博客各类统计信息接口
 *
 * @Author Zhao
 * @Create 2021/8/10 18:38
 */
@RestController
@RequestMapping("/blog")
public class BlogStatController extends BasicController {

    @Resource
    private RemoteArticleService remoteArticleService;
    @Resource
    private RemoteCommentService remoteCommentService;
    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 博客信息
     */
    @GetMapping("/info")
    public BaseResult<BlogInfoVo> getBlogInfo() {
        BlogInfoVo infoVo = new BlogInfoVo();
        infoVo.setBlogOnlineDate(DateUtils.daysBetween(new Date(), AppGlobalConstants.BLOG_ONLINE_DATE));
        // 可以自己写个 group by 偷懒了
        infoVo.setArticleCount(Optional.ofNullable(remoteArticleService.getArticleCount().getData()).orElse(0));
        infoVo.setCommentCount(Optional.ofNullable(remoteCommentService.countByType(CommentTypeEnum.COMMENT.getValue()).getData()).orElse(0));
        infoVo.setMessageCount(Optional.ofNullable(remoteCommentService.countByType(CommentTypeEnum.MESSAGE.getValue()).getData()).orElse(0));
        return BaseResult.<BlogInfoVo>success().data(infoVo).build();
    }

    /**
     * 微服务信息
     */
    @GetMapping("/serviceInfo")
    public BaseResult<Map<String, Integer>> getServiceInfo() {
        Map<String, Integer> serviceCountMap = getAliveServiceInfo();
        return BaseResult.<Map<String, Integer>>success().data(serviceCountMap).build();
    }

    private Map<String, Integer> getAliveServiceInfo() {
        Map<String, Integer> serviceCountMap = Maps.newHashMap();
        for (ServiceTypeEnum se : ServiceTypeEnum.validValues()) {
            // TODO 找一找 nacos 中有没有提供查询某个分组内所有微服务概况的 API 接口
            List<ServiceInstance> serviceInstances = discoveryClient.getInstances(se.getServiceId());
            serviceCountMap.put(se.getDesc(), Optional.ofNullable(serviceInstances).orElse(Lists.newArrayList()).size());
        }
        return serviceCountMap;
    }
}
